package com.furnitur.core.mapper.review;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.furnitur.core.entity.review.Review;
import com.furnitur.modules.portal.vo.review.ReviewVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

@Mapper
public interface ReviewMapper extends BaseMapper<Review> {
    
    /**
     * 分页查询评价列表
     */
    Page<ReviewVO> selectReviewPage(Page<ReviewVO> page, 
                                  @Param("furnitureId") Long furnitureId,
                                  @Param("filterType") String filterType,
                                  @Param("sortType") String sortType,
                                  @Param("userId") Long userId);

    /**
     * 根据ID查询评价详情
     */
    ReviewVO selectReviewById(@Param("id") Long id, @Param("userId") Long userId);

    /**
     * 增加点赞数
     */
    int incrementLikes(@Param("id") Long id);

    /**
     * 减少点赞数
     */
    int decrementLikes(@Param("id") Long id);

    /**
     * 查询评价统计信息
     */
    List<Map<String, Object>> selectReviewStats(@Param("furnitureId") Long furnitureId);

    /**
     * 根据商家ID删除评论点赞记录
     */
    @Delete("DELETE FROM review_likes WHERE review_id IN (SELECT id FROM furniture_reviews WHERE furniture_id IN (SELECT id FROM furniture WHERE merchant_id = #{merchantId}))")
    int deleteLikesByMerchantId(@Param("merchantId") Long merchantId);

    /**
     * 根据商家ID删除评论
     */
    @Delete("DELETE FROM furniture_reviews WHERE furniture_id IN (SELECT id FROM furniture WHERE merchant_id = #{merchantId})")
    int deleteByMerchantId(@Param("merchantId") Long merchantId);
}